package com.xingkong.web.controller.product;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.Arrays;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xingkong.common.annotation.Log;
import com.xingkong.common.enums.BusinessType;
import com.xingkong.product.domain.SpecificationItem;
import com.xingkong.product.service.SpecificationItemService;
import com.xingkong.common.core.controller.BaseController;
import com.xingkong.common.core.text.Convert;
import com.xingkong.common.core.domain.AjaxResult;
import com.xingkong.common.utils.poi.ExcelUtil;
import com.xingkong.common.core.page.TableDataInfo;

/**
 * 规格项值Controller
 *
 * @author yiyue
 * @date 2020-04-06
 */
@Controller
@RequestMapping("/product/item")
public class SpecificationItemController extends BaseController {
    private String prefix = "product/item";

    @Autowired
    private SpecificationItemService specificationItemService;

    @RequiresPermissions("product:item:view")
    @GetMapping()
    public String item(Long specId,ModelMap mmap) {
        mmap.put("specId",specId);
        return prefix + "/item";
    }

    /**
     * 查询规格项值列表
     */
    @RequiresPermissions("product:item:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SpecificationItem specificationItem) {
        startPage();
        return getDataTable(specificationItemService.list(new QueryWrapper<SpecificationItem>(specificationItem)));
    }

    @GetMapping("/getItemBySpecId/{specId}")
    @ResponseBody
    public List<SpecificationItem> listItemBySpecId( @PathVariable("specId") Long specId){
        SpecificationItem specificationItem = new SpecificationItem();
        specificationItem.setSpecificationId(specId);
        return specificationItemService.list(new QueryWrapper<SpecificationItem>(specificationItem));
    }

    /**
     * 导出规格项值列表
     */
    @RequiresPermissions("product:item:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SpecificationItem specificationItem) {
        List<SpecificationItem> list = specificationItemService.list(new QueryWrapper<>());
        ExcelUtil<SpecificationItem> util = new ExcelUtil<SpecificationItem>(SpecificationItem.class);
        return util.exportExcel(list, "item");
    }

    /**
     * 新增规格项值
     */
    @GetMapping("/add/{specificationId}")
    public String add(@PathVariable("specificationId") Long specificationId, ModelMap mmap) {
        mmap.put("specificationId", specificationId);
        return prefix + "/add";
    }

    /**
     * 新增保存规格项值
     */
    @RequiresPermissions("product:item:add")
    @Log(title = "规格项值", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SpecificationItem specificationItem) {
        return toAjax(specificationItemService.save(specificationItem));
    }

    /**
     * 修改规格项值
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SpecificationItem specificationItem = specificationItemService.getById(id);
        mmap.put("specificationItem", specificationItem);
        return prefix + "/edit";
    }

    /**
     * 修改保存规格项值
     */
    @RequiresPermissions("product:item:edit")
    @Log(title = "规格项值", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SpecificationItem specificationItem) {
        return toAjax(specificationItemService.updateById(specificationItem));
    }

    /**
     * 删除规格项值
     */
    @RequiresPermissions("product:item:remove")
    @Log(title = "规格项值", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(specificationItemService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }
}
